package com.ciwong.epaper.modules.me.ui;

import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.me.adapter.ClassListAdapterSpecific;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.IntentValue;
import com.ciwong.epaper.util.LogOutUtil;
import com.ciwong.mobilelib.bean.SchoolDetail;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.bean.Clazz;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.SerializableManager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 班级列表界面
 *
 * @author sugy
 * @version ciwong v.1.0 2015/9/10 15:42
 * @since ciwong v.1.0
 */
public class ClassListActivity extends BaseActivity {
//    private static final int REQUEST_CODE_TO_JOIN_CLASS = 1;

    private EApplication application;
    private ListView mListView;
    private ClassListAdapterSpecific mAdapter;
    private int jumpType;
    private String mTeacherPhoneNum;
    //要更换的班级
    private Clazz mClazz;

    @Override
    protected void preCreate() {
        super.preCreate();
        jumpType = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_TYPE,
                IntentValue.JumpToJoinClassType.TYPE_CHECK_CLASS_LIST);
        mClazz = (Clazz) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);

        if (mClazz != null) {
            Log.d("ciwong", "########class list page from class id#######" + mClazz.getId());
        }


        mTeacherPhoneNum = getIntent().getStringExtra(IntentFlag.INTENT_FLAG_STR);

    }

    @Override
    protected int setView() {
        return R.layout.activity_class_list;
    }

    @Override
    protected void findViews() {
        mListView = (ListView) findViewById(R.id.class_list);
    }

    @Override
    protected void initEvent() {

//        setRightBtnListener(new ViewOnClickListener()
//        {
//            @Override
//            public void avertRepeatOnClick(View v)
//            {
//                MeJumpManager.jumpToJoinClassActivity(R.string.go_back,
//                        ClassListActivity.this,
//                        IntentValue.JumpToJoinClassType.FROM_CLASS_LIST,
//                        REQUEST_CODE_TO_JOIN_CLASS);
//            }
//        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Clazz clazz = (Clazz) parent.getItemAtPosition(position);
                if (clazz != null && jumpType == IntentValue.JumpToJoinClassType.TYPE_CHECK_CLASS_LIST) {
                    //新需求改为跳转到详情
                    mAdapter.setSelectObject(clazz);
                    application.setClazz(clazz);
                    MeJumpManager.jumpToClassDetail(ClassListActivity.this, R.string.go_back, clazz, EpaperConstant.REQUEST_CODE_CLASS_JUMP_DETAIL_PAGE);
//                    SerializableManager.getInstance().serialize(
//                            SerializableManager.SerializeKey.SHARE_KEY_CLASS_DEFAULT,
//                            clazz);
//                    setResult(RESULT_OK);
                }
            }
        });
    }


    @Override
    protected void init() {
        setTitleText(R.string.clazz);
//        setRightBtnBG(R.drawable.add_join_class_selector);
        List<Clazz> clazzs = new ArrayList<Clazz>();
        application = (EApplication) getBaseApplication();
        Clazz clazz = application.getClazz();
        mAdapter = new ClassListAdapterSpecific(this, clazzs, jumpType, clazz, mClazz);
        mListView.setAdapter(mAdapter);
    }

    @Override
    protected void loadData() {
        if (null != mTeacherPhoneNum) {
            getNetClassListByMobile(mTeacherPhoneNum);
        } else {
            getLocalClassList();
        }
    }

    /**
     * 获取本地班级列表
     */
    private void getLocalClassList() {
        showMiddleProgressBar(getTitleText());
        SerializableManager.getInstance().deSerialize(
                SerializableManager.SerializeKey.SHARE_KEY_CLASS_LIST,
                new BaseCallBack() {


                    @Override
                    public void success(Object data) {
                        refreshUI((List<Clazz>) data);
                        getNetClassList();
                    }

                    @Override
                    public void failed(int errorCode, Object data) {
                        getNetClassList();
                    }

                    @Override
                    public void failed(Object data) {

                    }
                });
    }

    /**
     * 获取网络班级列表
     */
    private void getNetClassList() {
        SchoolDetail mySchool = EApplication.getInstance().getMySchool();
        if (null == mySchool) {
            return;
        }
        MeDao.getInstance().getClassList(EApplication.BRAND_ID, 2, mySchool.getSchoolId(), new BaseExtCallBack(ClassListActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
            @Override
            public void success(Object data) {
                List<Clazz> clazzs = (List<Clazz>) data;
                refreshUI(clazzs);
                // 更新本地班级列表
                SerializableManager.getInstance().serialize(
                        SerializableManager.SerializeKey.SHARE_KEY_CLASS_LIST,
                        (Serializable) clazzs);
                hideMiddleProgressBar();
            }

            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                hideMiddleProgressBar();
            }

            @Override
            public void failed(Object data) {
                hideMiddleProgressBar();
            }
        });
    }


    /**
     * 获取网络班级列表
     */
    private void getNetClassListByMobile(String mobileNum) {
        MeDao.getInstance().getClassListByTeacherPhoneNum(EApplication.BRAND_ID, mobileNum, new BaseExtCallBack(ClassListActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {

            @Override
            public void success(Object data) {
                List<Clazz> clazzs = (List<Clazz>) data;
                refreshUI(clazzs);
                Log.d("ciwong", "########clazzs.size()###########" + clazzs.size());
                // 更新本地班级列表
//                SerializableManager.getInstance().serialize(
//                        SerializableManager.SerializeKey.SHARE_KEY_CLASS_LIST,
//                        (Serializable) clazzs);
                hideMiddleProgressBar();
            }

            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                hideMiddleProgressBar();
            }

            @Override
            public void failed(Object data) {
                hideMiddleProgressBar();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case EpaperConstant.REQUEST_CODE_CLASS_JUMP_DETAIL_PAGE:// 加入班级成功后回调
                    setResult(RESULT_OK);

                    finish();

//                    showMiddleProgressBar(getTitleText());
//                    Log.d("ciwong", "#####刷新用户班级列表信息#######");
//                    getNetClassList();

                    break;

            }
        }
    }

    /**
     * 刷新界面
     *
     * @param clazzs 班级信息集
     */
    private void refreshUI(List<Clazz> clazzs) {
        mAdapter.clear();
        mAdapter.addAll(clazzs);
        Clazz clazz = (Clazz) mAdapter.getSelectObject();
        if (clazz == null) {
            if (clazzs != null && !clazzs.isEmpty()) {
                clazz = (Clazz) mAdapter.getItem(0);
            }
        } else {
            if (clazzs == null || !clazzs.contains(clazz)) {
                clazz = null;
            }
        }
        mAdapter.setSelectObject(clazz);
        application.setClazz(clazz);
        SerializableManager.getInstance().serialize(
                SerializableManager.SerializeKey.SHARE_KEY_CLASS_DEFAULT,
                clazz);
    }
}
